package com.vcit.system.service.impl;

import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import com.vcit.common.utils.DateUtils;
import com.vcit.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.vcit.system.mapper.TGroupClazzsMapper;
import com.vcit.system.domain.TGroupClazzs;
import com.vcit.system.service.ITGroupClazzsService;

/**
 * 班级群班级关系Service业务层处理
 * 
 * @author dff
 * @date 2025-01-17
 */
@Service
public class TGroupClazzsServiceImpl implements ITGroupClazzsService 
{
    @Autowired
    private TGroupClazzsMapper tGroupClazzsMapper;

    /**
     * 查询班级群班级关系
     * 
     * @param id 班级群班级关系主键
     * @return 班级群班级关系
     */
    @Override
    public TGroupClazzs selectTGroupClazzsById(Long id)
    {
        return tGroupClazzsMapper.selectTGroupClazzsById(id);
    }

    /**
     * 查询班级群班级关系列表
     * 
     * @param tGroupClazzs 班级群班级关系
     * @return 班级群班级关系
     */
    @Override
    public List<TGroupClazzs> selectTGroupClazzsList(TGroupClazzs tGroupClazzs)
    {
        return tGroupClazzsMapper.selectTGroupClazzsList(tGroupClazzs);
    }

    /**
     * 新增班级群班级关系
     * 
     * @param tGroupClazzs 班级群班级关系
     * @return 结果
     */
    @Override
    public int insertTGroupClazzs(TGroupClazzs tGroupClazzs)
    {
        tGroupClazzs.setCreateTime(DateUtils.getNowDate());
        tGroupClazzs.setCreateBy(SecurityUtils.getUsername());
        return tGroupClazzsMapper.insertTGroupClazzs(tGroupClazzs);
    }

    /**
     * 批量添加班级到班级群
     * 
     * @param cgId 班级群ID
     * @param clazzIds 班级ID列表
     * @return 结果
     */
    @Override
    @Transactional
    public int batchAddClazzsToGroup(Long cgId, List<Long> clazzIds)
    {
        if (clazzIds == null || clazzIds.isEmpty()) {
            return 0;
        }
        
        // 1. 查询已存在的班级群班级关系
        TGroupClazzs query = new TGroupClazzs();
        query.setCgId(cgId);
        List<TGroupClazzs> existingRelations = tGroupClazzsMapper.selectTGroupClazzsList(query);
        
        // 2. 提取已存在的班级ID
        List<Long> existingClazzIds = new ArrayList<>();
        if (existingRelations != null && !existingRelations.isEmpty()) {
            for (TGroupClazzs relation : existingRelations) {
                existingClazzIds.add(relation.getClazzId());
            }
        }
        
        // 3. 过滤出需要新增的班级ID
        List<Long> newClazzIds = new ArrayList<>();
        for (Long clazzId : clazzIds) {
            if (!existingClazzIds.contains(clazzId)) {
                newClazzIds.add(clazzId);
            }
        }
        
        // 4. 如果没有需要新增的班级，直接返回
        if (newClazzIds.isEmpty()) {
            return 0;
        }
        
        // 5. 批量插入新的班级群班级关系
        String username = SecurityUtils.getUsername();
        Date now = DateUtils.getNowDate();
        
        // 构建批量插入的对象列表
        List<TGroupClazzs> insertList = new ArrayList<>();
        for (Long clazzId : newClazzIds) {
            TGroupClazzs tGroupClazzs = new TGroupClazzs();
            tGroupClazzs.setCgId(cgId);
            tGroupClazzs.setClazzId(clazzId);
            tGroupClazzs.setCreateBy(username);
            tGroupClazzs.setCreateTime(now);
            insertList.add(tGroupClazzs);
        }
        
        // 执行批量插入
        return tGroupClazzsMapper.batchInsertTGroupClazzs(insertList);
    }

    /**
     * 修改班级群班级关系
     * 
     * @param tGroupClazzs 班级群班级关系
     * @return 结果
     */
    @Override
    public int updateTGroupClazzs(TGroupClazzs tGroupClazzs)
    {
        tGroupClazzs.setUpdateTime(DateUtils.getNowDate());
        tGroupClazzs.setUpdateBy(SecurityUtils.getUsername());
        return tGroupClazzsMapper.updateTGroupClazzs(tGroupClazzs);
    }

    /**
     * 批量删除班级群班级关系
     * 
     * @param ids 需要删除的班级群班级关系主键
     * @return 结果
     */
    @Override
    public int deleteTGroupClazzsByIds(Long[] ids)
    {
        return tGroupClazzsMapper.deleteTGroupClazzsByIds(ids);
    }

    /**
     * 删除班级群班级关系信息
     * 
     * @param id 班级群班级关系主键
     * @return 结果
     */
    @Override
    public int deleteTGroupClazzsById(Long id)
    {
        return tGroupClazzsMapper.deleteTGroupClazzsById(id);
    }
}
